#!/bin/bash

# Step 1: Generate files with corresponding content
s1_generate_a_series_of_files() {
    for index in $(seq 1 100); do
        echo "$index" > "/tmp/new_file$index"
    done
}

# Step 2: List files in long format about account and group
s2_list_4_files_in_long_format_about_account_and_group() {
    ls -l /etc/passwd /etc/group /etc/shadow /etc/gshadow | sort
}

# Step 3: Find items related to given username
s3_find_items_related_to_given_username() {
    local username="$1"
    (grep "^$username:" /etc/passwd; grep "^$username:" /etc/shadow) | sort
}

# Step 4: Find items related to given group name
s4_find_items_related_to_given_group_name() {
    local groupname="$1"
    (grep "^$groupname:" /etc/group; grep "^$groupname:" /etc/gshadow) | sort
}

# Step 5: Create groups
s5_create_groups_for_given_group_names() {
    groupadd rnd
    groupadd itoperation
}

# Step 6: Create users and set their groups as required
s6_create_users_and_set_their_group_as_required() {
    useradd -G rnd adam
    useradd -G rnd bob
    useradd -G itoperation calvin
    useradd -G itoperation david
    useradd -G rnd edward
}

# Step 7: Change users' shell
s7_change_users_shell() {
    chsh -s /usr/bin/sh adam
    chsh -s /usr/bin/sh edward
}

# Step 8: Set password change policy
s8_set_password_change_policy() {
    for user in adam bob calvin david edward; do
        chage -m 10 -M 60 -W 10 "$user"
    done
}

# Step 9: Set password for five users
s9_set_password_for_five_users() {
    local passwords=("$@")
    local usernames=(adam bob calvin david edward)
    for i in "${!usernames[@]}"; do
        echo "${usernames[$i]}:${passwords[$i]}" | chpasswd
    done
}

# Step 10: Lock Bob's password
s10_lock_bobs_password() {
    usermod -L bob
}

# Step 11: Include Adam into wheel group
s11_include_adam_into_wheel_group() {
    usermod -aG wheel adam
}

# Step 12: Change files permission
s12_change_files_permission() {
    for index in $(seq 3 3 100); do
        chmod 640 "/tmp/new_file$index"
    done
}

# To call each function, uncomment the desired function call below:
# s1_generate_a_series_of_files
# s2_list_4_files_in_long_format_about_account_and_group
# s3_find_items_related_to_given_username "username"
# s4_find_items_related_to_given_group_name "groupname"
# s5_create_groups_for_given_group_names
# s6_create_users_and_set_their_group_as_required
# s7_change_users_shel
